import json
import pyautogui
import time
from .Win import My_Window

pyautogui.FAILSAFE = False
#python -m caozuo.dianji

def json_data(gamexy_json_path):
    """
    读取游戏位置坐标json文件
    """
    with open(gamexy_json_path, 'r', encoding ='utf-8' ) as file:
        # 加载JSON数据
        data = json.load(file)
        data1 = data['game_xy']
        return data1


class Game_Dj:
    def __init__(self,title,gamexy_json_path = None):
        self.game_xy_data = json_data(gamexy_json_path)
        win1 = My_Window(title) # 创建一个My_Window对象，用于获取窗口位置
        xy = win1.get_window_rect() # 获取窗口位置
        self.x =xy[0]
        self.y =xy[1]
    
    def dianji(self,name,num = 1):
        game_x = self.game_xy_data[name][0]
        game_y = self.game_xy_data[name][1]

        dianji_x = self.x+ game_x
        dianji_y = self.y+ game_y
        for i in range(num):
            pyautogui.click(x = dianji_x, y = dianji_y)
            
      
        print(f"{name}点击成功")
    def tap_key(self,key):
        pyautogui.press(key)

